package com.rightsidetech.http.controller.admin.system;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.commom.utils.StringUtil;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.system.dtos.request.LoginAdminReq;
import com.rightsidetech.system.dtos.request.SignOutReq;
import com.rightsidetech.system.dtos.request.UpdatePasswordReq;
import com.rightsidetech.system.dtos.response.LoginResp;
import com.rightsidetech.system.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;

@RestController
@RequestMapping("/admin/sys_login")
@ShenyuSpringMvcClient(path = "/admin/sys_login/**")
@Api(tags = "admin:后台用户登录")
public class SysLoginController {
    @Resource
    private SysLoginService sysLoginService;
    @Resource
    private HttpServletRequest request;

    @Log
    @ApiOperation("后台用户登录")
    @PostMapping("login")
    public ResponseData<LoginResp> login(@RequestBody LoginAdminReq loginReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(loginReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysLoginService.login(loginReq, StringUtil.isNullorEmpty(request.getAttribute("ip"))? null:request.getAttribute("ip").toString());
    }

    @Log
    @Token
    @ApiOperation("退出登录")
    @PostMapping("signout")
    public ResponseData<LoginResp> logout(@RequestBody SignOutReq signOutReq) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(signOutReq);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysLoginService.logout(signOutReq.getToken());
    }

    @Log
    @Token
    @ApiOperation("修改密码")
    @PostMapping("/updatePwd")
    @ModifyLog(serviceClass = SysLoginService.class,idField = "token",idType = String.class)
    public ResponseData<LoginResp> updatePwd(@RequestBody UpdatePasswordReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysLoginService.updatePwd(req);
    }
}
